package com.yanqu.road.server.cmd.cross3.xrank.season;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.season.BaseSeasonFactory;
import com.yanqu.road.server.logic.season.BaseSeasonRank;
import com.yanqu.road.server.logic.season.BaseSeasonReward;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_XRANK_GET_SEASON_USER_RANK_LIST, desc = "获取赛季个人榜单")
public class GetXRankSeasonUserRankListCmd extends AbstractCommand {

    private int type;
    private int pageNo = 0;
    private int pageSize = 200;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXSeasonUserRankListReqMsg reqMsg = XRankProto.GetXSeasonUserRankListReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        if (reqMsg.hasPageNo()) {
            pageNo = reqMsg.getPageNo();
        }
        if (reqMsg.hasPageSize()) {
            pageSize = reqMsg.getPageSize();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 校验分页参数
        if (pageNo < 0 || pageSize < 0 || pageSize > 500) {
            ret = GameErrorCode.E_WASHE_PARAM_ERROR;
            return;
        }

        // 拿一下配置
        eSeasonRewardType enumType = eSeasonRewardType.getType(type);
        if (enumType == null || !enumType.isRankType()) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonReward tmpInstance = BaseSeasonFactory.getInstance(enumType, player);
        if (!(tmpInstance instanceof BaseSeasonRank)) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonRank instance = (BaseSeasonRank) tmpInstance;

        // 有没有赛季
        if (instance.getSeasonId() <= WasheConfig.SEASON_ID_NULL) {
            ret = GameErrorCode.E_SEASON_REWARD_SEASON_NO_EXIST;
            return;
        }

        long myValue = instance.getData().getValue();

        // 到跨服拿个人榜
        XRankProto.CrossGetXSeasonUserRankListReqMsg.Builder builder = XRankProto.CrossGetXSeasonUserRankListReqMsg.newBuilder();
        builder.setSystemId(instance.getSystemId());
        builder.setSeasonId(instance.getSeasonId());
        builder.setType(instance.getType().getIntValue());
        builder.setRangeKey(instance.getRangeKey());
        builder.setPageNo(pageNo);
        builder.setMyValue(myValue);
        builder.setPageSize(pageSize);
        player.sendPacket(Cross3Protocol.C3_XRANK_GET_SEASON_USER_RANK_LIST, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            XRankProto.GetXSeasonUserRankListRespMsg.Builder builder = XRankProto.GetXSeasonUserRankListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_XRANK_GET_SEASON_USER_RANK_LIST, builder);
        }
    }
}
